<?php

/**
 * Extension to HTML2PDF class used to parse Harliane's variables in tag :
 * &lt;HFIELD name="my.variable" /&gt;
 *
 * @author herve.guenot
 * @version $Id: HApplet.php,v 1.1 2011-08-10 05:21:47 herve Exp $
 * @copyright 2011
 */
class HApplet extends CWidget
{
	private $className;
	private $width;
	private $height;

	public function __construct($owner = null)
	{
		parent::__construct($owner);
		$this->setWidth("100%");
		$this->setHeight(400);
	}
	
	public function getClassName()
	{
		return $this->className;
	}
	
	public function setClassName($className)
	{
		$this->className = $className;
	}

	public function getWidth()
	{
		return $this->width;
	}
	
	public function setWidth($width)
	{
		if (!preg_match("/[\d]+/", $width))
			throw new CException(Yii::t(__CLASS__, 'width must be a string of digits'));
		$this->width = $width;
	}

	public function getHeight()
	{
		return $this->height;
	}
	
	public function setHeight($height)
	{
		if (!preg_match("/[\d]+/", $height))
			throw new CException(Yii::t(__CLASS__, 'height must be a string of digits'));
		$this->height = $height;
	}

	public function run() {
		$cs= Yii::app()->clientScript;
		$dir= dirname(__FILE__) . DIRECTORY_SEPARATOR . jars . DIRECTORY_SEPARATOR;

		// publishing jar files
		$baseUrl= Yii::app()->getAssetManager()->publish($dir, false, -1, true);
		
		if (!isset($this->className))
			throw new CException(Yii::t(__CLASS__, 'ClassName have to be set.'));
		
		$className = "_HApplet_" . $this->className;
		
		
		if (!class_exists($className))
			throw new CException($this->className . Yii::t(__CLASS__, ' can\'t be found have to be set.'));
		
		// retrieving linked libraries
		$lib_dir = $dir . "lib";
		$t = time();
		$t = date ("Ymd_His.", filemtime("$dir/harliane-applet.jar"));
		$classpath="$baseUrl/harliane-applet.jar?$t";
		if (is_dir($lib_dir)) 
		{
		    if ($dh = opendir($lib_dir)) 
		    {
		        while (($file = readdir($dh)) !== false) 
		        {
		        	if ($this->ends_with($file, ".jar"))
			            $classpath = "$classpath,$baseUrl/lib/$file";
		        }
		        closedir($dh);
		    }
		}		
			
		
		$c = new $className();
		
		$v = <<<APPLET
<applet 
	name="%s" 
	code="%s" 
	archive="$classpath"
	width="%s" 
	height="%d" mayscript>
	
	%s
	
</applet>
APPLET;
		
		$parameters = "";
		foreach ($c->getParameters() as $name => $value)
		{
			$value = htmlspecialchars($value);
			$parameters .= "\t<param name=\"$name\" value=\"$value\" />\n";
		}
		
		$v = sprintf($v, $this->className, $c->getClassName(), $this->width, $this->height, $parameters);
		echo $v;
		
		$c->addSpecificData();
	}

	private function ends_with($Haystack, $Needle)
	{
    	// Recommended version, using strpos
    	return strrpos($Haystack, $Needle) === strlen($Haystack)-strlen($Needle);
	}
}


class _HApplet_StepDesigner
{
	public function getClassName()
	{
		return 'fr.harlie.harliane.applet.StepDesigner.class';
	}

	public function getParameters()
	{
		$steps = Step::model()->findAll();

		$arr = array();


		foreach ($steps as $step)
		{
			$arr[] = array(
				'id' => $step->id, 
				'label' => $step->label, 
				'description' => $step->description,
				'active' => $step->active ? "true" : "false",
				'items' => $this->buildTopics($step->allTopics)
			);
		}

		$json = json_encode($arr);
		//$json = htmlspecialchars($json);
		
		return array('json' => $json, 'callback' => 'saveSteps');
	}
	
	public function addSpecificData()
	{
		$js= "function saveSteps(steps)\n{\n$('#json').attr('value', JSON.stringify(steps));$('#json').parent('form').submit();\n}\n";
		echo <<<SCRIPT
<script type="text/javascript"> 
$js
</script>
SCRIPT;

		echo CHtml::beginForm();
		echo "<input type=\"hidden\" id=\"json\" name=\"json\" value=\"\" />";
		echo CHtml::endForm();
	}

	private function buildTopics($topics)
	{
		$arr = array();

		foreach ($topics as $topic)
		{
			$arr[] = array(
				'id' => $topic->id, 
				'label' => $topic->label, 
				'description' => $topic->description,
				'active' => $topic->active ? "true" : "false",
				'items' => $this->buildTopics($topic->allTopics)
			);
		}

		return $arr;
	}

}

class _HApplet_RouteDesigner
{
	public function getClassName()
	{
		return 'fr.harlie.harliane.applet.RouteDesigner.class';
	}

	public function getParameters()
	{
		$steps = Step::model()->findAll();
		$routes = Route::model()->findAll();
		
		$arr = array();

		foreach ($steps as $step)
		{
			$arr[] = array(
				'id' => $step->id, 
				'label' => $step->label, 
				'description' => $step->description,
				'active' => $step->active ? "true" : "false"
			);
		}

		$steps = json_encode($arr);

		$arr = array();

		foreach ($routes as $route)
		{
			$a = array();
			foreach ($route->steps as $step) {
				$a[] = $step->id;
			}
			$arr[] = array(
				'mnemonic' => $route->mnemonic, 
				'label' => $route->label, 
				'description' => $route->description,
				'active' => $route->active ? "true" : "false",
				'steps' => $a
			);
			
		}

		$json = json_encode($arr);
		
		return array('steps' => $steps, 'json' => $json, 'callback' => 'saveRoutes');
	}
	
	public function addSpecificData()
	{
		$js= "function saveRoutes(routes)\n{\n$('#json').attr('value', JSON.stringify(routes));$('#json').parent('form').submit();\n}\n";
		echo <<<SCRIPT
<script type="text/javascript"> 
$js
</script>
SCRIPT;

		echo CHtml::beginForm();
		echo "<input type=\"hidden\" id=\"json\" name=\"json\" value=\"\" />";
		echo CHtml::endForm();
	}
	
}
